package com.demo.result.exception;


import com.demo.result.data.Result;
import com.demo.result.enums.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author： yu.Tan
 * @Date: 2020/11/23 14:48
 * @Description:
 */
@RestControllerAdvice
@Slf4j
public class RRExceptionHandler {

    /**
     * 自定义异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(RRException.class)
    public Result handleRRException(RRException e) {
        Result r = new Result();
        r.setCode(e.getCode());
        r.setMessage(e.getMsg());
        return r;
    }
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error(e.getMessage(), e);
        return Result.failure(ResultCode.ERROR,e.getMessage());
    }
    @ResponseBody
    @ExceptionHandler(ArithmeticException.class)
    public Result handleException(ArithmeticException e) {
        log.error(e.getMessage(), e);
        return Result.failure(ResultCode.ERROR,e.getMessage());
    }
}
